﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using DoAnWeb.DTO;

namespace DoAnWeb.DAO
{
    public class SanPhamDAO
    {
        DataProvider dp = new DataProvider();
        public BindingList<SanPhamDTO> GetList()
        {
            if (dp.OpenConnect())
            {
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_SanPham", null);
                dp.CloseConnect();
                BindingList<SanPhamDTO> List = new BindingList<SanPhamDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    SanPhamDTO DTO = new SanPhamDTO();
                    DTO.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                    DTO.TenSanPham = row["TenSanPham"].ToString();
                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TietKiem = int.Parse(row["TietKiem"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }
        public SanPhamDTO GetFromId(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaCTSanPham", _Ma) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetFromID_SanPham", null);
                dp.CloseConnect();
                SanPhamDTO DTO = new SanPhamDTO();
                foreach (DataRow row in dt.Rows)
                {
                    DTO.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                    DTO.TenSanPham = row["TenSanPham"].ToString();
                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TietKiem = int.Parse(row["TietKiem"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                }
                return DTO;
            }
            return null;
        }
        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaSanPham", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaSanPham", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
        public bool Add(SanPhamDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@TenSanPham", _DTO.TenSanPham), new SqlParameter("@TenTaiKhoan", _DTO.TenTaiKhoan), new SqlParameter("@HinhAnh", _DTO.HinhAnh) ,new SqlParameter("@GiaBan", _DTO.GiaBan),
                                                new SqlParameter("@MoTa", _DTO.MoTa), new SqlParameter("@TietKiem", _DTO.TietKiem), new SqlParameter("@NgayHetHan", _DTO.NgayHetHan), new SqlParameter("@DaXoa", _DTO.DaXoa)
                                              };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }
        public bool Update(SanPhamDTO _DTO)
        {
            bool kq;
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaSanPham", _DTO.MaSanPham), new SqlParameter("@TenSanPham", _DTO.TenSanPham), new SqlParameter("@TenTaiKhoan", _DTO.TenTaiKhoan), new SqlParameter("@HinhAnh", _DTO.HinhAnh) ,
                                                new SqlParameter("@GiaBan", _DTO.GiaBan), new SqlParameter("@MoTa", _DTO.MoTa), new SqlParameter("@TietKiem", _DTO.TietKiem), new SqlParameter("@NgayHetHan", _DTO.NgayHetHan), new SqlParameter("@DaXoa", _DTO.DaXoa)
                                              };
                kq = dp.SqlExcuteNonQuery("uproc_Update_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}